<?php  
class ControllerModuleColorFilter extends Controller {
	public $optionId = 5;
	protected function index() {
		$this->language->load('module/colorfilter');
		
    $this->data['heading_title'] = $this->language->get('heading_title');
		
		if (isset($this->request->get['color_id'])) {
			$parts = $this->request->get['color_id'];
		} else {
			$parts = '';
		}
		$this->data['color_id'] = $parts;
		
		$this->load->model('catalog/option');
		$this->load->model('catalog/product');
		
		$option_info = $this->model_catalog_option->getOptionValueDescriptions($this->optionId);
		$this->data['categories'] = array();
					
		$url = http_build_query($this->request->get);

		foreach ($option_info as $key => $category) {
			$children_data = array();
			
			$data = array(
				'filter_color_id'  => $category['option_value_id'],
				'filter_sub_category' => true
			);		
				
//			$product_total = $this->model_catalog_product->getTotalProducts($data);
						
			$children_data[] = array(
				'color_id'    =>$category['option_value_id'],
				'name'        => $category['option_value_description'][2]['name'],
				'href'        => $this->url->link('product/category', 'path=' . $category['option_value_id'])
			);
			
			$data = array(
				'filter_category_id'  => $category['option_value_id'],
				'filter_sub_category' => true	
			);		
				
			$product_total = $this->model_catalog_product->getTotalProducts($data);

			if(empty($parts)){
				$urlFilter = HTTP_SERVER.'index.php?'.$url.(empty($url)?'':'&').'color_id='.$category['option_value_id'];
			} else {
				$urlReplace = str_replace('color_id='.$parts,'color_id='.$category['option_value_id'],$url);
			  $urlFilter = HTTP_SERVER.'index.php?'.$urlReplace;
			} 
			$this->data['categories'][] = array(
				'color_id'    => $category['option_value_id'],
				'name'        => $category['option_value_description'][2]['name'],
				'children'    => $children_data,
				'href'        => $urlFilter
			);
		}
		
		if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/module/colorfilter.tpl')) {
			$this->template = $this->config->get('config_template') . '/template/module/colorfilter.tpl';
		} else {
			$this->template = 'default/template/module/colorfilter.tpl';
		}
		
		$this->render();
  	}
}
?>